package com.atguigui.leetcode1;

/**
 * 2609.最长平衡子字符串
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2023/11/8 20:12
 */
public class P2609FindTheLongestBalancedSubstringOfABinaryString {
    public static void main(String[] args) {
        Solution solution = new P2609FindTheLongestBalancedSubstringOfABinaryString().new Solution();
        // TO TEST
        solution.findTheLongestBalancedSubstring("0011010");
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findTheLongestBalancedSubstring(String s) {
            if (s.equals("")) return 1;

            int ans = 0;
            int a = 0, b = 0;
            boolean flag = true;
            int n = s.length();
            for (int i = 0; i < n; i++) {

                if(s.charAt(i) == '0'){
                    if (flag){
                        a++;
                    }else {
                        flag = true;
                        ans = Math.max(ans, Math.min(a, b) * 2);
                        a = 1;
                        b = 0;
                    }
                }else {
                    flag = false;
                    b++;
                }
            }


            return Math.max(ans, Math.min(a, b) * 2);
        }
    }
}
